# -*- coding: utf-8 -*-
# @Time    : 2020/7/8 15:09
# @Author  : ZHALONG
# @FileName: 妹子图.py
# @Software: PyCharm

import requests
import sys
from parsel import Selector
from faker import Faker

user_agent = Faker("zh-CN").user_agent()
headers = {
    "User-Agent": user_agent,
    "referer": "https://www.mzitu.com/"
}
image_urls = []


def get_response(url):
    try:
        response = requests.get(url, headers)
        response.encoding = response.apparent_encoding
        print(response.status_code)
        if response.status_code == 200:
            return response
    except Exception as e:
        print("错误原因：" + str(e.args))
        sys.exit()

def image_url(response):
    selector = Selector(response.text)
    li_list = selector.xpath("//div[@class='postlist']/ul/li")
    for li in li_list:
        image_url = li.xpath("./span[1]/a/@href").get()
        image_urls.append(image_url)

def image(image_urls):
    for url in image_urls:
        response = get_response(url)
        selector = Selector(response.text)
        page = selector.xpath("//div[@class='pagenavi']/a[last()-1]/span/text()").get()
        for i in range(1, int(page) + 1):
            page_url = url + str(i)
            image_res = get_response(url=page_url)
            xpath = Selector(image_res.text)
            image_href = xpath.xpath("//div[@class='main-image']/p/a/img/@src").get()
            image_name = image_href.split("/")[-1]
            with open(image_name, 'ab') as f:
                f.write(requests.get(image_href).content)

if __name__ == '__main__':
    meizitu = "https://www.mzitu.com/"
    response = get_response(url=meizitu)
    image_url(response)
    image(image_urls)